package com.example.lovedogs.fragment.shop;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.lovedogs.MainActivity;
import com.example.lovedogs.R;
import com.example.lovedogs.domain.goodsclass;
import com.example.lovedogs.shop.GoodsListActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.event.OnItemLongClick;

import android.R.array;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts.Data;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ShangchengRightFragment extends Fragment {
	private static final String String = null;
	private View view;
	private GridView lv1;
	private MainActivity mainActivity;
	private String url = "http://10.0.2.2:8080/LoveDogs04/JsonGoodskindsServlet";
	private RequestQueue rq;
	private MyAdapter adapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (view == null) {
			view = inflater.inflate(R.layout.fragmentright_shangcheng,
					container, false);
		}
		return view;
	}

	public void updateListViewRightFragment(String data) {
		lv1 = (GridView) view.findViewById(R.id.listView1_fr1);
		mainActivity = (MainActivity) getActivity();
		initGridView(data);
	}

	// 初始化GridView
	private void initGridView(String data) {
		// 1
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", data);
		// 2
		HttpUtils http = new HttpUtils();
		// 3
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				List<goodsclass> ldata = new ArrayList<goodsclass>();
				ldata.add(new goodsclass(1, "呕吐"));
				ldata.add(new goodsclass(2, "腹泻"));
				ldata.add(new goodsclass(3, "食欲下降"));
				ldata.add(new goodsclass(4, "咳嗽"));
				ldata.add(new goodsclass(5, "瘙痒"));
				ldata.add(new goodsclass(6, "营养不良"));
				ldata.add(new goodsclass(7, "厌食"));
				ldata.add(new goodsclass(8, "贫血"));
				ldata.add(new goodsclass(9, "心动过快"));
				ldata.add(new goodsclass(10, "多饮"));
				ldata.add(new goodsclass(11, "呼吸困难"));
				ldata.add(new goodsclass(12, "腹痛"));
				ldata.add(new goodsclass(13, "失明"));
				ldata.add(new goodsclass(13, "眼睛出血"));
				/*
				 * Gson g=new Gson(); Type type=new
				 * TypeToken<List<Goodskinds>>(){}.getType(); List<Goodskinds>
				 * ldata=g.fromJson(responseInfo.result, type);
				 */
				if (adapter == null) {
					adapter = new MyAdapter(ldata);
				} else {
					adapter.setData(ldata);
				}
				adapter.notifyDataSetChanged();
				lv1.setAdapter(adapter);
				//调用setOnItemClickListener()方法实现单击选项！！！
				lv1.setOnItemClickListener(new MyOnItemClickListener());
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				Toast.makeText(getActivity(), "error", Toast.LENGTH_LONG)
						.show();
			}
		});
	}

	// 自定义适配器
	class MyAdapter extends BaseAdapter {
		private List<goodsclass> data;
		private LayoutInflater inflater;
		private SpannableStringBuilder sp;
		
		public MyAdapter(List<goodsclass> data) {
			this.data = data;
			inflater = LayoutInflater.from(getActivity());
		}

		public List<goodsclass> getData() {
			return data;
		}

		public void setData(List<goodsclass> data) {
			this.data = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		public SpannableStringBuilder getSp() {
			return sp;
		}

		public void setSp(SpannableStringBuilder sp) {
			this.sp = sp;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			goodsclass info = data.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(
						R.layout.listitem_shangchengzhengzhuang_item, null);
			}
			TextView textView1 = (TextView) convertView
					.findViewById(R.id.textView1);
			textView1.setText(info.getName());
			return convertView;
		}
	}
	// 自定义点击事件实现类
	private class MyOnItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			goodsclass sdemo = (goodsclass) adapter.getItem(position);
			int no = sdemo.getNo();
			SpannableStringBuilder sp=adapter.getSp();
			String GoodsTheme = sdemo.getName();
			if (position==0) {
				Intent intent = new Intent(getActivity(), GoodsListActivity.class);
				intent.putExtra("no", no);
				intent.putExtra("sp", sp);
				startActivity(intent);
			}
		}

	}
}
